package android.slc.module.project.utils;

import android.slc.app.startup.TaskConstant;
import android.slc.app.startup.task.TaskSlc;
import android.slc.app.startup.task.TaskThirdParty;
import android.slc.appbase.utils.AppPathUtils;
import android.slc.appbase.utils.process.ProcessUtils;
import android.slc.box.anchors.task.Task;
import android.slc.box.anchors.task.TaskCreator;
import android.slc.box.anchors.task.project.Project;
import android.slc.jxbrowser.JxBrowserUtils;
import android.slc.jxbrowser.JxInit;
import android.slc.module.project.ui.browser.AppJxBrowserUtils;
import android.slc.utils.SlcFileResourceUtils;
import android.slc.utils.SlcFileUtils;
import cn.hutool.core.io.FileUtil;
import org.jetbrains.annotations.NotNull;

public class DelayTaskUtils {

    public static Project.Builder delayTaskBuilder() {
        Project.Builder builder = new Project.Builder(TaskConstant.PROJECT_DELAY_PROJECT, new Project.TaskFactory(new TaskDelayCreate()));
        builder.add(TaskConstant.TASK_THIRD_PART);
        builder.add(TaskConstant.TASK_SLC).dependOn(TaskConstant.TASK_THIRD_PART);
        builder.add(InsureProcessUniqueTask.TASK_ID)
                .dependOn(TaskConstant.TASK_SLC)
                .add(TaskLastTask.TASK_ID)
                .dependOn(InsureProcessUniqueTask.TASK_ID);
        return builder;
    }

    public static class TaskDelayCreate implements TaskCreator {

        public TaskDelayCreate() {
        }

        @NotNull
        @Override
        public Task createTask(@NotNull String s) {
            switch (s) {

                case TaskConstant.TASK_THIRD_PART:
                    return new TaskThirdParty();

                case TaskConstant.TASK_SLC:
                    return new TaskSlc();

                case InsureProcessUniqueTask.TASK_ID:
                    return new InsureProcessUniqueTask();

                case TaskLastTask.TASK_ID:
                    return new TaskLastTask();

            }
            throw new IllegalStateException("task name is not defined");
        }
    }

    public static class InsureProcessUniqueTask extends Task {
        public static final String TASK_ID = "insureProcessUniqueTask";

        public InsureProcessUniqueTask() {
            super(TASK_ID, true);
        }

        @Override
        protected void run(@NotNull String name) {
            ProcessUtils.killOldPidByPName();
        }
    }

    public static class TaskLastTask extends Task {
        public static final String TASK_ID = "lastResource";

        public TaskLastTask() {
            super(TASK_ID, true);
        }

        @Override
        protected void run(@NotNull String name) {
            //复制模版资源
            SlcFileResourceUtils.copyResource("assets/templates", FileUtil.file(AppPathUtils.getTemplatesPath()));
            //初始化浏览器
            String userDataDirTemp = SlcFileUtils.complementSeparator(AppJxBrowserUtils.JX_BROWSER_USER_DATA);
            FileUtil.del(userDataDirTemp + "auto_generation");
            //初始化浏览器
            JxInit.init7_22();
            JxBrowserUtils.browserHaMaxCount = 0;
            JxBrowserUtils.userDataDir = AppJxBrowserUtils.JX_BROWSER_USER_DATA;
            JxBrowserUtils.initHardwareAccelerated();
        }
    }

}
